package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyue.eam.admin.api.entity.regi.CqCompanyEntity;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.excel.annotation.ResponseExcel;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.regi.service.CqCompanyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公司表
 *
 * @date 2023-11-17 15:34:46
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqCompany")
@Tag(description = "cqCompany", name = "公司表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqCompanyController {
    private final CqCompanyService cqCompanyService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param cqCompany 公司表
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    // @PreAuthorize("@pms.hasPermission('eam_cqCompany_view')" )
    public R getCqCompanyPage(@ParameterObject Page page, @ParameterObject CqCompanyEntity cqCompany) {
        LambdaQueryWrapper<CqCompanyEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(cqCompanyService.page(page, wrapper));
    }


    /**
     * 通过id查询公司表
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(cqCompanyService.getById(id));
    }

    /**
     * 通过用户所在租户获取公司信息
     */
    @GetMapping
    @Operation(summary = "通过用户所在租户获取公司信息", description = "通过用户所在租户获取公司信息")
    public R getCompanyInfo() {
        return R.ok(cqCompanyService.getCompanyInfo());
    }


    /**
     * 新增公司表
     * @param cqCompany 公司表
     * @return R
     */
   /*  @Operation(summary = "新增公司表" , description = "新增公司表" )
    @SysLog("新增公司表" )
    @PostMapping
    public R save(@RequestBody CqCompanyEntity cqCompany) {
        return R.ok(cqCompanyService.save(cqCompany));
    }
 */

    /**
     * 修改公司表
     *
     * @param cqCompany 公司表
     * @return R
     */
    @Operation(summary = "修改公司表", description = "修改公司表")
    @SysLog("修改公司表")
    @PutMapping
    public R updateById(@RequestBody CqCompanyEntity cqCompany) {
        return R.ok(cqCompanyService.updateCompany(cqCompany));
    }


    /**
     * 通过id删除公司表
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除公司表", description = "通过id删除公司表")
    @SysLog("通过id删除公司表")
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(cqCompanyService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param cqCompany 查询条件
     * @param ids       导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<CqCompanyEntity> export(CqCompanyEntity cqCompany, Long[] ids) {
        return cqCompanyService.list(Wrappers.lambdaQuery(cqCompany).in(ArrayUtil.isNotEmpty(ids), CqCompanyEntity::getId, ids));
    }
}
